package com.bwie.service.impl;

import cn.hutool.core.util.ObjectUtil;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.conditions.query.LambdaQueryChainWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.bwie.login.AdminLoginVo;
import com.bwie.poi.StringUtils;
import com.bwie.pojo.TbUser;
import com.bwie.service.TbUserService;
import com.bwie.mapper.TbUserMapper;
import com.bwie.status.ChackEnum;
import com.bwie.utils.R;
import com.bwie.vo.PageInfoVo;
import io.jsonwebtoken.Jwts;
import io.jsonwebtoken.SignatureAlgorithm;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;
import java.util.concurrent.TimeUnit;

/**
* @author 31803
* @description 针对表【tb_user(用户表)】的数据库操作Service实现
* @createDate 2025-04-21 16:11:23
*/
@Service
public class TbUserServiceImpl extends ServiceImpl<TbUserMapper, TbUser>
    implements TbUserService{

    @Autowired
    TbUserMapper tbUserMapper;

    @Autowired
    StringRedisTemplate redisTemplate;

    @Autowired
    HttpServletRequest request;

    public R getuserByid(Integer userId) {
        TbUser tbUser = tbUserMapper.selectById(userId);
        if(tbUser==null){
            return R.ERROR(404,"用户不存在");
        }
        tbUser.setUserPass(null);
        return R.OK(tbUser);
    }

    public R AdminLogin(AdminLoginVo adminLoginVo) {
        LambdaQueryWrapper<TbUser> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(TbUser::getUserName,adminLoginVo.getUsername());

        TbUser tbUser = tbUserMapper.selectOne(wrapper);
        if(ObjectUtil.isEmpty(tbUser)){
            return R.ERROR(500,"用户不存在");
        }
        if(!adminLoginVo.getUserpass().equalsIgnoreCase(tbUser.getUserPass())){
            return R.ERROR(500,"密码不正确");
        }
        String token = Jwts.builder().signWith(SignatureAlgorithm.HS256, "123456")
                .claim("userId", tbUser.getUserId())
                .claim("userName", tbUser.getUserName())
                .claim("nickName", tbUser.getNickName()).compact();
        redisTemplate.opsForValue().set("token"+tbUser.getUserId(),token,60, TimeUnit.MINUTES);
tbUser.setToken(token);
tbUser.setUserPass(null);
        return R.OK(tbUser);
    }

    public R UserList(PageInfoVo pageInfoVo) {
        Page<TbUser> page = new Page<>(pageInfoVo.getPageNum(),pageInfoVo.getPageSize());
       QueryWrapper<TbUser> wrapper = new QueryWrapper<TbUser>();
        if(StringUtils.isNotEmpty(pageInfoVo.getKeyWord())){
            wrapper.lambda().like(TbUser::getUserName,pageInfoVo.getKeyWord()).or().like(TbUser::getNickName,pageInfoVo.getKeyWord()).or().like(TbUser::getUserEmail,pageInfoVo.getKeyWord());
        }


        wrapper.lambda().orderByDesc(TbUser::getUserId);
        Page<TbUser> tbUserPage = tbUserMapper.selectPage(page, wrapper);

        return R.OK(tbUserPage);
    }

    public R UserRge(TbUser tbUser) {
        //--校验用户是否重复
        TbUser one = tbUserMapper.selectOne(
                new QueryWrapper<TbUser>().lambda().eq(TbUser::getUserName, tbUser.getUserName())
        );
        if(one!=null){
        return R.ERROR(500,"用户名重复");
        }

        tbUser.setCreateTime(new Date());
        tbUserMapper.insert(tbUser);
        return R.OK();
    }

    public R checkStatus(TbUser tbUser) {
        //判断用户是否存在
        TbUser user = tbUserMapper.selectById(tbUser.getUserId());
        if(ObjectUtil.isEmpty(user)){
            return R.ERROR(500,"用户不存在");
        }
        //如果审核驳回给出审核理由
        if(tbUser.getUserStatus().equals(ChackEnum.CHACK_NO.getCode())){
            if(StringUtils.isEmpty(tbUser.getReason())){
                return R.ERROR(500,"审核驳回必须填写理由");
            }
        }


        //审核
        user.setUserStatus(tbUser.getUserStatus());
        user.setReason(tbUser.getReason());
        tbUserMapper.updateById(user);
        return R.OK();
    }

    public R GetMyinfo() {
        String token = request.getHeader("token");
        if(token==null){
            return R.ERROR(500,"token不存在");
        }

        Integer userId = Jwts.parser().setSigningKey("123456").parseClaimsJws(token).getBody().get("userId", Integer.class);
        TbUser tbUser = tbUserMapper.selectById(userId);

        return R.OK(tbUser);
    }

    public R updataUser(TbUser tbUser) {
        TbUser tbUser1 = tbUserMapper.selectById(tbUser.getUserId());
        if(ObjectUtil.isEmpty(tbUser1)){
            return R.ERROR(500,"用户不存在");
        }
        tbUserMapper.updateById(tbUser);
        return R.OK();
    }
}




